/**
 *@description 字段校验
 */

/**
 *@description 名称校验
 */
export const noChinese = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    //   console.log(option, value);
    let message = undefined;
    let regExp = /^[^\u4e00-\u9fa5]+$/;
    if (!regExp.test(value)) {
      message = "不能输入汉字";
    }
    console.debug(value, message);
    callback(message);
  }
};

/**
 *@description 名称校验
 */
export const name = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    //   console.log(option, value);
    let message = undefined;
    if (value && (value.length < 1 || value.length > 24)) {
      message = "字符长度为1至24";
    }
    // let regExp = new RegExp(/^[a-zA-Z\u4e00-\u9fa5][0-9A-Za-z_@\u4e00-\u9fa5]*$/);
    // if (!regExp.test(value)) {
    //   message = "以字母或汉字开头,由字母、汉字、数字、@、下划线组成。";
    // }
    let regExp = new RegExp(/^[0-9A-Za-z\u4e00-\u9fa5]*$/);
    if (!regExp.test(value)) {
      message = "只能输入字母、汉字、数字";
    }
    callback(message);
  }
};

export const nameLess = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    //   console.log(option, value);
    let message = undefined;
    if (value && (value.length < 2 || value.length > 50)) {
      message = "字符长度为2至50";
    }
    let regExp = new RegExp(/^[a-zA-Z][0-9A-Za-z_]*$/);
    if (!regExp.test(value)) {
      message = "以字母开头,由字母、数字、下划线组成。";
    }
    callback(message);
  }
};

/**
 *@description 编码校验
 */
export const code = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    //   console.log(option, value);
    let message = undefined;
    if (value && (value.length < 4 || value.length > 32)) {
      message = "字符长度为4至32位";
    }
    let regExp = new RegExp(/^[0-9A-Za-z]*$/);
    if (!regExp.test(value)) {
      message = "由字母、数字组成。";
    }
    callback(message);
  }
};

/**
 *@description 邮件地址校验
 */
export const email = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    ////   console.log(option, value);
    let regExp = /^([a-z0-9A-Z_-]+[_-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$/;
    let message = undefined;
    if (!regExp.test(value)) {
      message = "邮件地址格式错误";
    }
    callback(message);
  }
};

/**
 *@description 手机号码校验
 */
export const phone = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    ////   console.log(option, value);
    let regPhone = /^134[0-9]\d{7}$|^13[^4]\d{8}$|^14[0-9]\d{8}$|^15[^4]\d{8}$|^16[6]\d{8}$|^17[0-9]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/;
    // let regMobile = /^1(3|4|5|6|7|8|9)\d{9}$/;
    let message = undefined;
    if (!regPhone.test(value)) {
      message = "电话号码格式错误";
    }
    callback(message);
  }
};

/**
 *@description ipv4地址校验
 */
export const ipv4 = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    ////   console.log(option, value);
    let regExp =
      /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    let message = undefined;
    if (!regExp.test(value)) {
      message = "地址为ipv4地址";
    }
    callback(message);
  }
};

/**
 *@description 端口校验
 */
export const port = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    ////   console.log(option, value);
    let message = undefined;
    if (!(0 <= value && value < 65536)) {
      message = "端口范围(0-65535)";
    }
    callback(message);
  }
};

/**
 *@description ipv4地址及端口校验
 */
export const ipv4AndPort = {
  // rule: this.$util.regExp.name,
  validator(option, value, callback) {
    ////   console.log(option, value);
    let arr = (value || "").split(":");
    let ip = arr[0];
    let port = arr[1];
    let regExp =
      /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
    let message = undefined;
    if (!regExp.test(ip) || !(0 <= port && port < 65536)) {
      message = "连接地址为IP(ipv4):端口(0-65535)";
    }
    callback(message);
  }
};
